<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" th:href="@{/css/bootstrap.min.css}">
    <link rel="stylesheet" type="text/css" th:href="@{/css/userRole.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/css/style_table.css}"/>
</head>
<style type="text/css">
    .form-horizontal {
        margin: 0;
        padding: 20px 50px
    }

    .form-group label {
        margin-top: 7px
    }

    .close_btn{
        height: 30px;
        width: 55px;
        position: absolute;
        right: 80px;
    }

</style>
<body>

<form id="fm" class="form-horizontal" novalidate>
    <div class="form-group">
        <div class="col-xs-9">
            <input type="hidden" name="id" id="id" class="form-control">
        </div>
    </div>
    <div class="form-group">
        <label class="col-xs-3 control-label">姓名:</label>
        <div class="col-xs-9">
            <input type="text" name="studentName" id="studentName" class="form-control ">
        </div>
    </div>
    <div class="form-group">
        <label class="col-xs-3 control-label">家庭住址:</label>
        <div class="col-xs-9">
            <input type="text" name="address" id="address" class="form-control ">
        </div>
    </div>
    <div class="form-group">
        <label class="col-xs-3 control-label">亲情号码:</label>
        <div class="col-xs-9">
            <input type="text" name="familiarityNumber" id="familiarityNumber" class="form-control ">
        </div>
    </div>
    <div class="form-group">
        <label class="col-xs-3 control-label">就读方式:</label>
        <div class="col-xs-9">
            <input type="text" name="enrollmentType" id="enrollmentType" class="form-control ">
        </div>
    </div>
    <div class="form-group">
        <label class="col-xs-3 control-label">所在班级:</label>
        <div class="col-xs-9">
            <select id='classId' name='classId' class="classId">
                <option value="" selected>未选择</option>
            </select>
        </div>
    </div>
    <button id="save" class="btn btn-primary" type="button" onclick="updateStudent();">保存</button>
    <button id="hide" class="btn btn-warning close_btn" type="button" onclick="parent.closeWindow();">关闭</button>
</form>

</body>
<script type="text/javascript" th:src="@{/js/jquery.min.js}"></script>
<script type="text/javascript" th:src="@{/plugin/easyui/jquery.easyui.min.js}"></script>
<script type="text/javascript" th:src="@{/plugin/layer/layer.js}"></script>
<script th:inline="javascript">
    $(function () {
        var studentId = sessionStorage.getItem("studentId");
        $("#id").val(studentId);
        console.log("studentId=" + studentId);
        //加载下拉框
        $.post("/class/getAllClass", function (data) {
            if (data.msg == 200) {
                for (var i = 0; i < data.classList.length; i++) {
                    $(".classId").append("<option value=" + data.classList[i].id + ">" + data.classList[i].className + "</option>");
                }
            } else {
                layer.msg(data.msg);
            }

        }, "json");

        //填充表单
        $.ajax({
            url: "/student/getStudentById",
            type: "GET",
            data: {
                "id" : studentId
            },
            dataType: "json",
            success: function (result) {
                if (result.msg === 200) {
                    $("#studentName").val(result.data.studentName);
                    $("#address").val(result.data.address);
                    $("#id").val(result.data.id);
                    $("#familiarityNumber").val(result.data.familiarityNumber);
                    $("#enrollmentType").val(result.data.enrollmentType);
                    var ids1 = $("#classId").find("option"); //获取select下拉框的所有值
                    parent.selectNumber(ids1, result.data.classId);

                }
            }
        });

    });


    /**
     * 空值校验
     * @returns {boolean}
     */
    function checkForm() {
        var studentName = $("#studentName").val();
        var address = $("#address").val();
        var studentId = $("#id").val();
        var familiarityNumber = $("#familiarityNumber").val();
        var enrollmentType = $("#enrollmentType").val();
        var classId = $("#classId").val();
        if(parent.isEmpty(studentName)){
            layer.msg("请输入姓名");
            return false
        }else if(parent.isEmpty(address)){
            layer.msg("请输入家庭住址");
            return false
        }else if(parent.isEmpty(studentId)){
            layer.msg("未获取到学号");
            return false
        }else if(parent.isEmpty(familiarityNumber)){
            layer.msg("请输入亲情号码");
            return false
        }else if(parent.isEmpty(classId)){
            layer.msg("请选择班级");
            return false
        }else if(parent.isEmpty(enrollmentType)){
            layer.msg("请输入就读方式");
            return false
        }
        return true;
    }

    /**
     * 提交表单方法
     */
    function updateStudent() {
        if(checkForm()){
            $.ajax({
                type: "POST",
                dataType : "json",
                url: "/student/updateStudent",
                data: $("#fm").serialize(),
                success: function (result) {
                    if(result.msg === 200){
                        parent.searchStudent(15,1);
                        parent.closeWindow("修改成功");
                    }else{
                        layer.msg(result.msg);
                    }
                }
            });
        }

    }

</script>
</html>